<template>
  <div class="choose-course">
    <div class="table-data">
      <div class="heard-container">
        <el-row :style="{display:'flex',alignItems:'center'}">
          <el-col :span="5">
            <div class="search-inner-item">
              <el-checkbox v-model="sort" :true-label="1" :false-label="0" checked>仅显示未在本专区添加的资源
              </el-checkbox>
            </div>
          </el-col>
          <el-col
            v-if="classifyType === 'RWFL'"
            :span="8"
            :style="{marginBottom:0,display: 'flex',alignItems: 'center'}"
          >
            <div class="search-inner-item">
              <span class="h-label">人物类型：</span>
              <el-select v-model="craftsmanType" @change="handleChange">
                <el-option
                  v-for="item in OptionsRWFL"
                  :key="item.dictValue"
                  :value="item.dictValue"
                  :label="item.dictName"
                />
              </el-select>

            </div>
          </el-col>
          <el-col :span="8" :style="{marginBottom:0,display: 'flex',alignItems: 'center'}" v-if="classifyType">
            <span class="h-label"> 选择分类：</span>
            <el-cascader
              ref="classify"
              v-model="classifyId"
              clearable
              :props="createClassifyProps"
              :options="classifyOptions"
            />
          </el-col>
        </el-row>
      </div>
      <el-collapse-transition>
        <section class="main-search merge">
          <span class="svg-container">
            <svg-icon icon-class="search" />
          </span>
          <el-input v-model.trim="listQuery.name" placeholder="输入名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
          <section class="search-btn">
            <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button size="small" type="primary" plain @click="handleSetTag">批量添加标签</el-button>
          </section>
        </section>
      </el-collapse-transition>
      <v-table-child
        :list="tableData"
        :tag="tagList[urlType]"
        :selection="selection"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :source-arr="['subjectImgUrl', 'courseImgUrl', 'imgIdUrl', 'courseImgUrl', 'imgURL','coverImgIdUrl','headImgURL']"
        :is-child="isChild"
        @selectSub="handleSelect"
        @handleSelectionChange="handleSelectionChange"
      />
    </div>
    <div class="page">
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <!--    设置标签-->
    <el-dialog
      v-el-drag-dialog
      :modal="false"
      :close-on-click-modal="false"
      :visible.sync="classifyDialogVisible"
      title="设置标签"
      center
      width="700px"
    >
      <tag v-if="classifyDialogVisible" v-model="tags" :parentref="true" :tags-list="tagsList" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="classifyDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSettingTags">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryPage, getClassifyTree, getClassifyTreeRWFL } from '../api'
import Pagination from '@/components/Pagination'
import VTableChild from './Table'
import Tag from '../components/tag'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'ChooseCourse',
  components: { Pagination, VTableChild, Tag },
  directives: { elDragDialog },
  props: {
    courseClassify: {
      type: String,
      default: ''
    },
    isChild: {
      type: Boolean,
      default: false
    },
    tag: {
      // 表头
      type: String,
      default: ''
    },
    urlType: {
      // 资源分类
      type: String,
      default: ''
    },
    selection: {
      // 是否允许选择
      type: Boolean,
      default: true
    },
    areaId: {
      // 培训班id
      type: String,
      default: ''
    },
    isGet: {
      type: Boolean,
      default: true
    },
    classifyType: {
      // 分类标识
      type: String,
      default: ''
    }
  },
  data() {
    return {
      listLoading: false,
      // definition: '1',
      // getReward: '0',
      total: 0,
      tableData: [],
      listQuery: {
        page: 1,
        limit: 10
      },
      tagList: {
        // ZYLX_SP: 'contentVideoLibrarRelease', // 视频
        ZYLX_YP: 'courseManagement', // 音频
        ZYLX_ZL: 'documentLibraryLibrarRelease', // 资料
        ZYLX_XW: 'zone-news', // 新闻
        ZYLX_ZB: 'liveIndex', // 直播
        // ZYLX_HD: 'documentLibraryLibrarRelease', // 活动
        ZYLX_KS: 'examManagement', // 考试
        ZYLX_CG: 'resourceAddRecruit', // 闯关
        ZYLX_ZQ: 'documentLibraryLibrarRelease', // 专区
        ZYLX_TP: 'imgLibrary', // 图片
        ZYLX_ZXDT: 'newSpecial', // 专项答题
        ZYLX_DCWJ: 'surveyMain', // 调查问卷
        ZYLX_TPHD: 'vote', // 投票活动
        ZYLX_KC: 'courseManagement', // 课程
        ZYLX_RW: 'characterlibrary-artist' // 人物
      },
      multipleSelection: [],
      sort: '',
      createClassifyProps: {
        label: 'classifyName',
        value: 'id',
        checkStrictly: true
      },
      classifyOptions: [],
      classifyId: [],
      craftsmanType: '',
      OptionsRWFL: '',
      classifyDialogVisible: false,
      tagsList: '',
      tags: '',
      tagStr: ''
    }
  },
  computed: {},
  watch: {
    tags() {
      this.$emit('handleChangeTag', this.tags)
    }
  },
  created() {
    // if (this.isGet) {
    // this.getList()
    // }
  },
  methods: {
    handleSetTag(row) {
      this.classifyDialogVisible = true
    },
    handleSettingTags() {
      this.tagsList = this.tags
      this.classifyDialogVisible = false
    },
    // 获取人物分类数据
    getClassifyTreeRWFL() {
      return new Promise((resolve, reject) => {
        getClassifyTreeRWFL({ dictCode: 'RWFL' })
          .then(({ data }) => {
            this.OptionsRWFL = data.data && data.data.length > 0 ? data.data[0].list : []
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    handleChange() {
      this.getClassifyOption()
    },
    /**
     * 获取分类数据
     */
    getClassifyOption() {
      if (!this.classifyType) return
      let classifyType = ''
      if (this.classifyType !== 'RWFL') {
        classifyType = this.classifyType
      } else {
        classifyType = this.craftsmanType
      }
      let url = ''
      if (this.classifyType === 'XWFL_XWGL') {
        url = '/backend/newsClassify/getClassifyTree'
      } else if (this.classifyType === 'RWFL') {
        url = '/backend/craftsmanLibraryClassify/getClassifyTree'
      } else {
        url = '/backend/learnClassify/getClassifyTree'
      }
      return new Promise((resolve, reject) => {
        getClassifyTree({ classifyType: classifyType, status: 'ZT01' }, url)
          .then(({ data }) => {
            this.classifyOptions = data.data || []
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    handleSelect(info) {
      this.$emit('select', { arr: info })
    },
    handleSelectionChange(arr) {
      this.$emit('selectMany', { arr })
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      this.craftsmanType = ''
      this.classifyId = []
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        areaId: this.areaId,
        type: this.urlType,
        selectType: '1',
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        name: this.listQuery.name,
        craftsmanType: this.craftsmanType || '',
        sort: this.sort,
        classifyId: this.classifyId[this.classifyId.length - 1]
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.tableData = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },
    handleRegionList(row) {
      const arr = []
      if (row.areaProvinceName) {
        arr.push(row.areaProvinceName)
      }
      if (row.areaCityName) {
        arr.push(row.areaCityName)
      }
      if (row.areaCountyName) {
        arr.push(row.areaCountyName)
      }
      return arr.join('/')
    },
    filtercolumn(scope, item) {
      if (item === 'nd') {
        return scope.row['param2']
      } else if (item === 'EachDistrict') {
        return this.handleRegionList(scope.row)
      }
      return scope.row[item]
    }
  }
}
</script>

<style type="text/css" lang="scss">
.choose-course {
  height: 550px;
  display: flex;
  flex-direction: column;
  .heard-container {
    .h-label {
      font-weight: bold;
    }

  }
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .table-data {
    flex: 1;
  }
  .page {
    height: 50px;
  }
}
</style>
